/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, removing the copyright
 * infomation is regarded as invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa.util;

//import com.mr.qa.UserSessionUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory
{

    private static final ThreadLocal threadLocal = new ThreadLocal();
    private static org.hibernate.SessionFactory sessionFactory;
    private static final Log log = LogFactory.getLog(HibernateSessionFactory.class);
    static
    {
        try
        {
            log.info("initiate hibernate....");
            sessionFactory = new Configuration().configure().buildSessionFactory();
            log.info("initiate hibernate end");
        }
        catch(Exception ex)
        {
            log.error(ex);
            sessionFactory = null;
        }
    }    
    
    private HibernateSessionFactory() 
    {
    }
	
    /**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException 
    {
        Session session = (Session) threadLocal.get();
        if (session == null || !session.isOpen())
        {
            if (sessionFactory == null)
            {
                    rebuildSessionFactory();
            }
            session = (sessionFactory != null) ? sessionFactory.openSession(): null;
            threadLocal.set(session);
        }
        return session;
    }

    /**
     *  Rebuild hibernate session factory
     *
     */
    public static void rebuildSessionFactory() throws HibernateException
    {
        try
        {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        }
        catch(HibernateException he)
        {
            log.error(he);
            throw he;
        }
        catch(Exception e)
        {
            log.error(e);
            throw new HibernateException(e);
        }
    }

    /**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
    */
    public static void closeSession() throws HibernateException 
    {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);
        if(session != null)session.close();
    }

    /**
     *  return session factory
     *
     */
    public static org.hibernate.SessionFactory getSessionFactory() 
    {
		return sessionFactory;
    }
    
    
    
    //the transaction needs a new ThreadLocal
    
    public static Transaction beginTransaction1() {

          Transaction transaction = (Transaction) threadLocal.get();

          if (transaction == null) {

           transaction = getSession().beginTransaction();

           threadLocal.set(transaction);

          }
          return transaction;

         }

//the below is newly added, not used

    public static void commitTransaction() {

       Transaction transaction = (Transaction) threadLocal.get();

       threadLocal.set(null);

       if (transaction != null)

        transaction.commit();

      }

    public static void rollbackTransaction() 
    {

     Transaction tx = (Transaction) threadLocal.get();

     threadLocal.set(null);

     if (tx != null)

      tx.rollback();

    }

    
    
}